package com.car.android.sdk.bridge;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.car.android.sdk.CarBusiness;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.LinkedList;

public class CarCocosBridge implements CarCocosIAP {

    private static CarCocosBridge callbackApi;
    public static WeakReference<Activity> mainGameActivity;
    private Method evalString;
    private Method runOnGLThread;
    private static Handler handler = new Handler(Looper.getMainLooper());
    private volatile boolean GAME_ACTIVITY_PAUSED = false;
    private LinkedList<CarCocosCallback> callbackList = new LinkedList<>();

    public static CarCocosBridge getInstance() {
        if (callbackApi == null) {
            callbackApi = new CarCocosBridge();
        }
        return callbackApi;
    }

    public void setMainGameActivity(Activity activity) {
        mainGameActivity = new WeakReference<>(activity);
        callbackList.clear();
    }

    @Override
    public void onMainGameActivityResume() {
        GAME_ACTIVITY_PAUSED = false;
        if (callbackList != null && !callbackList.isEmpty()){
            while (callbackList.size() > 0){
                CarCocosCallback callback = callbackList.removeFirst();
                XSSdkCallback(callback.action, callback.jsonData);
            }
        }
    }

    @Override
    public void onMainGameActivityPause() {
        GAME_ACTIVITY_PAUSED = true;
    }

    private CarCocosBridge() {
        try {
            Class clzBridge = Class.forName("org.cocos2dx.lib.busbus.Cocos2dxJavascriptJavaBridge", false, CarBusiness.context.getClassLoader());
            evalString = clzBridge.getDeclaredMethod("evalString", String.class);
            evalString.setAccessible(true);

            Class clzCocos2dAct = Class.forName("org.cocos2dx.lib.busbus.Cocos2dxActivity", false, CarBusiness.context.getClassLoader());
            runOnGLThread = clzCocos2dAct.getDeclaredMethod("runOnGLThread", Runnable.class);
            runOnGLThread.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void XSSdkCallback(String action, String jsonData) {
        if (mainGameActivity == null || mainGameActivity.get() == null) {
            return;
        }
        handler.post(() -> {
            if (GAME_ACTIVITY_PAUSED){
                CarCocosCallback carCocosCallback = new CarCocosCallback();
                carCocosCallback.action = action;
                carCocosCallback.jsonData = jsonData;
                callbackList.add(carCocosCallback);
                return;
            }
            try {
                runOnGLThread.invoke(mainGameActivity.get(), new Runnable() {
                    @Override
                    public void run() {
                        try {
                            evalString.invoke(null, String.format("window.XSSdkCallback('%s','%s')", action, jsonData));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}
